package com.glink.manage.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.BaseCommonUtils;
import com.glink.common.util.LogUtil;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.dto.upload.*;
import com.glink.manage.service.AttachmentService;
import com.glink.manage.vo.upload.AttachmentAppVO;
import com.glink.manage.vo.upload.AttachmentImageUploadVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文档资料和附件等上传
 *
 * @author rencl
 */
@Slf4j
@RestController
@RequestMapping("/API/Manage/attachment")
@Api(tags = "文档资料和附件等上传")
public class AttachmentController extends AbstractBaseController {

    @Autowired
    private AttachmentService attachmentService;
 
    @ApiOperation(value = "01.静态图片上传,后台会自动压缩,无任何业务逻辑关系", notes = "01.支持jpg,jpeg,png格式")
    @RequestMapping(path = "/static/upload", method = RequestMethod.POST)
    public ResponseResult<List<String>> staticUpload(@Validated UploadImageListDTO dto) throws GeneralSimpleException {
        log.info("01.静态图片上传,后台会自动压缩    ------  ");
        String userId = BaseCommonUtils.extractUserIdtByToken();
        TimeInterval timer = DateUtil.timer();
        List<String> ret = attachmentService.onlyUploadStaticImages(dto, userId);
        log.info("01.静态图片上传,后台会自动压缩 {} ms------  ", timer.interval());
        return this.response(ret);
    }

    @ApiOperation(value = "01-2.静态图片上传,后台会自动压缩,无任何业务逻辑关系", notes = "01-2.支持jpg,jpeg,png格式")
    @RequestMapping(path = "/static/uploadOne", method = RequestMethod.POST)
    public ResponseResult<String> staticUploadOne(@Validated UploadImageDTO dto) throws GeneralSimpleException {
        log.info("01-2.静态图片上传,后台会自动压缩    ------  ");
        String userId = BaseCommonUtils.extractUserIdtByToken();
        TimeInterval timer = DateUtil.timer();
        String ret = attachmentService.onlyUploadStaticImagesOne(dto, userId);
        log.info("01-2.静态图片上传,后台会自动压缩 {} ms------  ", timer.interval());
        return this.response(ret);
    }

    @ApiOperation(value = "02.静态根据图片ID查询图片(raw目录,原图)", notes = "02.静态根据图片ID查询图片")
    @GetMapping(path = "/static/raw/{imageId}")
    public void findImageByIdRaw(@Validated @PathVariable("imageId") String imageId, HttpServletResponse httpServletResponse) throws Exception {
        log.info("02.静态根据图片ID查询图片: {}", imageId);
        attachmentService.exportStaticFile(imageId, "raw", httpServletResponse);
    }

    @ApiOperation(value = "03.静态根据图片ID查询图片(pre目录,压缩图)", notes = "03.静态根据图片ID查询图片")
    @GetMapping(path = "/static/pre/{imageId}")
    public void findImageByIdPre(@Validated @PathVariable("imageId") String imageId, HttpServletResponse httpServletResponse) throws Exception {
        log.info("03.静态根据图片ID查询图片: {}", imageId);
        attachmentService.exportStaticFile(imageId, "pre", httpServletResponse);
    }

    @ApiOperation(value = "04.静态附件上传,无任何业务逻辑关系")
    @RequestMapping(path = "/static/uploadAttachment", method = RequestMethod.POST)
    public ResponseResult<AttachmentImageUploadVO> staticUploadAttachment(@Validated UploadImageDTO dto) throws GeneralSimpleException {
        log.info("04.静态附件上传,无任何业务逻辑关系    ------  ");
        String userId = BaseCommonUtils.extractUserIdtByToken();
        TimeInterval timer = DateUtil.timer();
        AttachmentImageUploadVO uploadVO = attachmentService.onlyUploadStaticFile(dto, userId);
        log.info("04.静态附件上传,无任何业务逻辑关系 {} ms------  ", timer.interval());
        return this.response(uploadVO);
    }

    @ApiOperation(value = "05.静态根据图片ID查询附件")
    @GetMapping(path = "/static/attachment/{imageId}")
    public void findImageById(@Validated @PathVariable("imageId") String imageId, HttpServletResponse httpServletResponse) throws Exception {
        log.info("05.静态根据图片ID查询附件: {}", imageId);
        attachmentService.exportStaticFile(imageId, "raw", httpServletResponse);
    }

    @ApiOperation(value = "06.静态上传附件APP")
    @RequestMapping(path = "/static/uploadApp", method = RequestMethod.POST)
    public ResponseResult<Boolean> uploadApp(@Validated UploadAppDTO dto) throws GeneralSimpleException {
        log.info("06.静态上传附件APP    ------  ");
        TimeInterval timer = DateUtil.timer();
        Boolean status = attachmentService.uploadApp(dto);
        log.info("06.静态上传附件APP {} ms------  ", timer.interval());
        return this.response(status);
    }

    @ApiOperation(value = "07.查询APP版本信息")
    @GetMapping(path = "/getVersionByApp")
    public ResponseResult<AttachmentAppVO> getVersionByApp() throws GeneralSimpleException {
        log.info("07.查询APP版本信息    ------  ");
        TimeInterval timer = DateUtil.timer();
        AttachmentAppVO appVO = attachmentService.listByApp();
        log.info("07.查询APP版本信息 {} ms------  ", timer.interval());
        return this.response(appVO);
    }

    @ApiOperation(value = "08.下载FTP文件")
    @PostMapping(value = "/downloadPic")
    public void downloadPic(@RequestBody @Validated UploadFilePathDTO dto, HttpServletResponse response) throws Exception {
        log.info("08.下载文件：{}", LogUtil.encode(dto));
        attachmentService.downloadPicByFtp(dto, response);
    }

    @ApiOperation(value = "09.删除APP")
    @PostMapping(value = "/delApp", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> delApp(@RequestBody @Validated UploadAppDelDTO dto) throws GeneralSimpleException {
        log.info("09.删除APP,{}", LogUtil.encode(dto));
        TimeInterval timer = DateUtil.timer();
        Boolean status = attachmentService.delApp(dto);
        log.info("09.删除APP {} ms------  ", timer.interval());
        return this.response(status);
    }
}